package com.xuzimian.globaldemo.spring.core.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AspectForArgNames {


    @Pointcut(value = "execution(* com.xuzimian.globaldemo.spring.core.aop.pointcut.*.*(..)) && args(param1,param2)", argNames = "param1,param2")
    public void pointCut(Object p1, Object p2) {
        System.out.println("AOP pointCut 做了些什么？");
    }

    @Before(value = "pointCut(p1,p2)", argNames = "p1,p2")
    public void doBefore(JoinPoint joinPoint, Object p1, Object p2) {
        System.out.println("AOP Before Advice... p1=" + p1 + " p2=" + p2);
    }

    @After(value = "pointCut(p1,p2)", argNames = "p1,p2")
    public void doAfter(JoinPoint joinPoint, Object p1, Object p2) {
        System.out.println("AOP After Advice... " + joinPoint.getArgs() + "---p1=" + p1 + " p2=" + p2);
    }

    @AfterReturning(pointcut = "pointCut(p1,p2)", returning = "returnVal", argNames = "returnVal,p1,p2")
    public void afterReturn(JoinPoint joinPoint, Object returnVal, Object p1, Object p2) {
        System.out.println("AOP AfterReturning Advice:" + returnVal + p1 + " " + p2);
    }

    @AfterThrowing(pointcut = "pointCut(p1,p2)", throwing = "error", argNames = "error,p1,p2")
    public void afterThrowing(JoinPoint joinPoint, Throwable error, Object p1, Object p2) {
        System.out.println("AOP AfterThrowing Advice..." + error);
        System.out.println("AfterThrowing... " + "---p1=" + p1 + " p2=" + p2);
    }

    @Around(value = "pointCut(p1,p2)", argNames = "p1,p2")
    public void around(ProceedingJoinPoint pjp, Object p1, Object p2) {
        System.out.println("AOP Aronud before..." + "---p1=" + p1 + " p2=" + p2);
        try {
            pjp.proceed();
        } catch (Throwable e) {

        }
        System.out.println("AOP Aronud after..." + "---p1=" + p1 + " p2=" + p2);
    }

}
